package br.edu.utfpr.md.apps;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import br.edu.utfpr.md.beans.Carro;

public class TelaSerealiza extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton botao = new JButton();
	private JTextArea modelo = new JTextArea();
	private JTextArea marca = new JTextArea();
	private JTextArea ano = new JTextArea();
	private JTextArea preco = new JTextArea();
	private JLabel lbModelo = new JLabel("Modelo");
	private JLabel lbMarca = new JLabel("Marca");
	private JLabel lbAno = new JLabel("Ano");
	private JLabel lbPreco = new JLabel("preco");
	
	public TelaSerealiza(){
		this.setLocationRelativeTo(null);
		this.setLayout(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		initComponents();
			
	}
	
	public void initComponents(){
		modelo.setBounds(20, 30, 200, 20);
		marca.setBounds(20,70,200,20);
		ano.setBounds(20,110,200,20);
		preco.setBounds(20,150,200,20);
		
		lbModelo.setBounds(20,10,200,20);
		lbMarca.setBounds(20, 50, 200, 20);
		lbAno.setBounds(20,90,200,20);
		lbPreco.setBounds(20,130,200,20);
		
		botao.setText("gravar");
		botao.setBounds(20, 190, 200, 30);
		
		this.add(botao);
		botao.addActionListener(new ActionListener() { 
			@Override
			public void actionPerformed(ActionEvent e) {
				Carro ooComJava = new Carro();
				
				ooComJava.setModelo((modelo.getText()));
				ooComJava.setAno(Integer.parseInt(ano.getText()));
				ooComJava.setMarca(marca.getText());
				ooComJava.setPreco(Double.parseDouble(preco.getText()));
				
				FileOutputStream outFile;
				ObjectOutputStream serializer;
				try {
					
					outFile = new FileOutputStream(modelo.getText()+".txt");
					serializer= new ObjectOutputStream(outFile);
					serializer.writeObject(ooComJava);
					JOptionPane.showMessageDialog(null, "ok");
					dispose();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
			}
		});
	
		
		this.add(modelo);
		this.add(marca);
		this.add(ano);
		this.add(preco);
		this.add(lbModelo);
		this.add(lbAno);
		this.add(lbMarca);
		this.add(lbPreco);

	}
	
	public static void main(String[] args) {
		TelaSerealiza tela = new TelaSerealiza();
		tela.setTitle("Veiculos");
		tela.setSize(250, 260);
		tela.setVisible(true);
	}
	

}
